$$.ajax.Connection = $$.Class($$.Component, {
	url: null
	,method: $$.Ajax.DEFAULT_METHOD
	,params: null
	,xmlData: null
	,jsonData: null
	,timeout: $$.Ajax.DEFAULT_TIMEOUT
	,caching: $$.Ajax.DEFAULT_CACHING
	,complete: $$.emptyFn
	,success: $$.emptyFn
	,failure: $$.emptyFn
	
	,constructor: function(config){
		$$.put(this, config);
		this.addEvents(
			'before'
			,'complete'
			,'success'
			,'failure'
		);
		
		this.__parent.constructor.call(this, config);
	}
	,getXhr: function(){
		if(this.xhr) return this.xhr;
		
		if(window.XMLHttpRequest)
			this.xhr = new XMLHttpRequest();
			
		else if(window.ActiveXObject)
			this.xhr = new ActiveXObject('Microsoft.XMLHTTP');
			
		else
			$$.error("Cannot create an XMLHTTP instance");
			
		//if(this.xhr.overrideMimeType)
			//this.xhr.overrideMimeType('text/xml');
			
		return this.xhr;
	}
	,setHeader: function(name, content){
		this.getXhr().setRequestHeader(name, content);
		return this;
	}
	,request: function(config){
		var me = this;
		if($$.VALUE_HALT === this.fireEvent('before', me)) return false;
		
		$$.put(me, config);
		
		var xhr = me.getXhr()
			,url = me.url
			,method = me.method.toUpperCase()
			,data = me.xmlData? me.xmlData : me.jsonData? me.jsonData : me.params? $$.urlEncode(me.params) : ''
			,headerValue = 'text/html';
		
		
		xhr.onreadystatechange = function(a){
			if(4 === xhr.readyState){
				me.fireEvent('complete', me, xhr);
				me.complete(xhr);
				if(200 === xhr.status){
					me.fireEvent('success', me, xhr);
					me.success(xhr);
				}else{
					me.fireEvent('failure', me, xhr);
					me.failure(xhr);
				}
			}
		}
		
		if('GET' == method && me.params){
			url = $$.urlAppend(url, me.params);
		}
		
		xhr.open(me.method, url, true);
		
		if(me.xmlData) headerValue = 'text/xml';
		else if(me.jsonData) headerValue = 'application/json';
		else if('POST' == method) headerValue = 'application/x-www-form-urlencoded';
		
		me.setHeader('Content-Type', headerValue);
		
		xhr.send(data);
		return this;	
	}
	,abort: function(){
		this.getXhr().abort();	
	}
});